package com.cskaoyan.service.adminservice.impl;

import com.cskaoyan.bean.chaosbean.stat.vo.AssistOrderVO;
import com.cskaoyan.bean.chaosbean.stat.vo.SGoodsVO;
import com.cskaoyan.bean.chaosbean.stat.vo.SOrdersVO;
import com.cskaoyan.bean.chaosbean.stat.vo.SUserVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.service.adminservice.StatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Field;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jackX
 * @datetime 2022/12/31 11:11
 * @description:
 */
@Component
public class StatServiceImpl implements StatService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public SUserVO selectUser() {
        SUserVO userVO = new SUserVO();
        List<String> list = new ArrayList<>();
        List<SUserVO.RowsBean> rowsBean = userMapper.SelectUser();

        userVO.setRows(rowsBean);
        SUserVO.RowsBean bean = new SUserVO.RowsBean();
        Class<? extends SUserVO.RowsBean> aClass = bean.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            list.add(name);
        }
        userVO.setColumns(list);
        return userVO;
    }

    @Override
    public SOrdersVO selectOrder() {
        SOrdersVO vo = new SOrdersVO();
        List<String> list = new ArrayList<>();
        //查询出订单量 订单总金额 客单价  对应日期
        List<SOrdersVO.RowsBean> beans = orderMapper.selectOrder();
        //下单用户单独查询，进行日期匹配单独赋值
        List<AssistOrderVO> orderVOS = orderMapper.selectConfimUser();
        for (int i = 0; i < beans.size(); i++) {

            for (int j = 0; j < orderVOS.size(); j++) {
                if (beans.get(i).getDay().equals(orderVOS.get(j).getDay())) {
                    beans.get(i).setCustomers(orderVOS.get(j).getCustomers());
                }
            }
            if (beans.get(i).getCustomers() == null) {
                beans.get(i).setCustomers(0);
            }
        }
        SOrdersVO.RowsBean rowsBean = new SOrdersVO.RowsBean();
        Class<? extends SOrdersVO.RowsBean> aClass = rowsBean.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            list.add(name);
        }

        vo.setRows(beans);
        vo.setColumns(list);
        return vo;
    }

    @Override
    public SGoodsVO selectGoods() {
        SGoodsVO vo = new SGoodsVO();
        List<String> list = new ArrayList<>();
        for (Field field : new SGoodsVO.RowsBean().getClass().getDeclaredFields()) {
            list.add(field.getName());
        }
        List<SGoodsVO.RowsBean> beans = goodsMapper.selectGoods();
        vo.setColumns(list);
        vo.setRows(beans);
        return vo;
    }
}
